#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct {
	char name[10];
	int price;
} goods;
typedef struct node {
	struct node *next;
	goods gos;
} node;
typedef struct stack {
	struct node *head;
	struct node *tail;
	int len;
	int max;
} stack;
stack *stackmaker() {
	stack *s = (stack *)malloc(sizeof(stack));
	s->head = NULL;
	s->tail = NULL;
	s->len = 0;
	s->max = 0;
	return s;
}
node *nodemaker(char *name, int price) {
	node *n = (node *)malloc(sizeof(node));
	n->next = NULL;
	strcpy(n->gos.name, name);
	n->gos.price = price;
	return n;
}
int get_max(stack *s, int price) {
	if (price > s->max)
		s->max = price;
	else if (s->len == 0)
		return -1;

}
int push(stack *s, node *n) {
	if (s->len == 0)
		s->head = n; //只有一项就作为头节点
	else
		s->tail->next = n;
	s->tail = n;//不论只有一个节点还是多个，最后都更新尾节点为新插入的节点
	s->len++;
	get_max(s, n->gos.price);
}
void traverse(stack *s) {
	node *n = s->head;
	for (int i = 0; i < s->len; i++) {
		printf("商品 %d : %s ,价格 : %d\n", i + 1, n->gos.name, n->gos.price);
		n = n->next;
	}
}
node *add(int price) {
	switch (price) {
		case 5:
			return nodemaker("可乐", 5);
			break;
		case 4:
			return nodemaker("雪碧", 4);
			break;
		case 3:
			return nodemaker("矿泉水", 3);
			break;
		case 2:
			return nodemaker("纸巾", 2);
			break;
	}

}
int removed(stack *s,int sum) {
	if (!s->len)
		return -1;
	node *deln = s->head;
	sum-=s->head->gos.price;
	s->head = s->head->next;
	deln->next = NULL;
	free(deln);
	s->len--;
	printf("删除第一位商品成功\n");
	return sum;
}
void clearstack(stack *s) {
	while (s->head != NULL) {
		node *temp = s->head;
		s->head = s->head->next;
		free(temp);
	}
	s->len = 0;
	s->tail = NULL;
}
stack *finish(stack *s) {
	if(s->len==0){
		printf("当前购物车为空，请先选购后再结算\n");
		return NULL;
	}
	int sum = 0, fun;
	node *n = s->head;
	for (int i = 0; i < s->len; i++) {
		sum += n->gos.price;
		n = n->next;
	}
	over:
	printf("当前购物车内包含：\n");
	traverse(s);
	printf("总计：%d , 移除第一个商品输入1,若无则输入0确认支付: ", sum);
	scanf("%d", &fun);
	if (fun == 0) {
		printf("谢谢惠顾，是否需要继续购物（否购物车将自动清空）1.是 0.否 ：");
		scanf("%d", &fun);
		if (fun == 0) {
			clearstack(s);
			free(s);
			return NULL;
		} else if (fun == 1)
			return s;
	} else if (fun == 1) {
redelfir:
		if ((sum=removed(s,sum) )== -1){
			printf("购物车为空\n");
			goto over;
		}
		printf("是否需要继续删除：1.是 0.否: ");
		scanf("%d", &fun);
		if (fun == 1)
			goto  redelfir;
		else if (fun == 0)
			goto over;
	}
}
int main() {
	int fun, price;
	stack *s = NULL;
			printf("欢迎使用自助结算系统\n");
	while (1) {
		if(s==NULL)
			s = stackmaker();
		
		printf("选择功能：\n1.选购 2.查看购物车 3.结算 :");
		scanf("%d", &fun);
		switch (fun) {
			case 1:
				system("cls");
rechoose:
				printf("请输入对应物品的价格:\n可乐 ￥5\n雪碧 ￥4\n矿泉水 ￥3\n纸巾 ￥2\n请选择: ");
				scanf("%d", &price);
				if(price>5||price<2){
				printf("输入有误，请重新输入\n");
				system("cls");
				goto rechoose;
				}
				node *newnode = add(price);
				if (newnode != NULL)
					push(s, newnode);
				printf("是否需要继续选购:1.是 0.否 :");
				scanf("%d", &fun);
				if (fun == 1)
					goto rechoose;
				else if (fun == 0)
					break;
			case 2:
				system("cls");
				if (s->len == 0) {
					printf("当前购物车为空\n");
					break;
				}
				printf("当前购物车商品：\n");
				traverse(s);
				printf("\n当前商品最高价格为：%d , 注意适度消费\n是否现在结算：1.是 0.否: ", s->max);
				scanf("%d", &fun);
				if (fun == 1)
				goto pay;
				else if (fun == 0)
					break;
			case 3:
pay:			system("cls");
				printf("正在结算：\n");
				s = finish(s);
				break;
		}
	}
	return 0;
}
